#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e5 + 5;

int n, m;
struct Node {
  int val, cnt;
  int sz, fa;
  int ch[2];
} tr[N];
int root;
#define lc(x) tr[x].ch[0]
#define rc(x) tr[x].ch[1]
int newnode(int v) {
  static int tot = 0;
  ++tot;
  tr[tot].val = v;
  tr[tot].cnt = tr[tot].sz = 1;
  tr[tot].fa = tr[tot].ch[0] = tr[tot].ch[1] = 0;
  return tot;
}
void pushup(int x) { tr[x].sz = tr[lc(x)].sz + tr[rc(x)].sz + tr[x].cnt; }
void rotate(int x) {
  int y = tr[x].fa, z = tr[y].fa;
  int k = tr[y].ch[1] == x;
  int k1 = tr[z].ch[1] == y;
  tr[z].ch[k1] = x, tr[x].fa = z;
  tr[y].ch[k] = tr[x].ch[k ^ 1], tr[tr[x].ch[k ^ 1]].fa = y;
  tr[x].ch[k ^ 1] = y, tr[y].fa = x;
  pushup(y), pushup(x);
}
void splay(int x, int k) {
  while (tr[x].fa != k) {
    int y = tr[x].fa, z = tr[y].fa;
    if (z != k) {
      if ((rc(y) == x) ^ (rc(z) == y))
        rotate(x);
      else
        rotate(y);
    }
    rotate(x);
  }
  if (!k) root = x;
}
void insert(int v) {
  int x = root, p = 0;
  while (x && tr[x].val != v) {
    p = x, x = tr[x].ch[tr[x].val < v];
  }
  if (x)
    tr[x].cnt++;
  else {
    x = newnode(v);
    tr[p].ch[tr[p].val < v] = x;
    tr[x].fa = p;
  }
  splay(x, 0);
}
void find(int v) {
  int x = root;
  while (tr[x].val != v && tr[x].ch[tr[x].val < v]) x = tr[x].ch[tr[x].val < v];
  splay(x, 0);
}
int get_pre(int v) {
  find(v);
  int x = root;
  if (tr[x].val < v) return x;
  x = lc(x);
  while (rc(x)) x = rc(x);
  splay(x, 0);
  return x;
}
int get_suc(int v) {
  find(v);
  int x = root;
  if (tr[x].val > v) return x;
  x = rc(x);
  while (lc(x)) x = lc(x);
  splay(x, 0);
  return x;
}
void del(int v) {
  int pre = get_pre(v);
  int suc = get_suc(v);
  splay(pre, 0), splay(suc, pre);
  int x = tr[suc].ch[0];
  if (tr[x].cnt > 1)
    tr[x].cnt--, splay(x, 0);
  else
    tr[suc].ch[0] = 0, splay(suc, 0);
}
int get_rk(int v) {
  insert(v);
  int res = tr[lc(root)].sz;
  del(v);
  return res;
}
int get_val(int k) {
  int x = root;
  while (true) {
    if (tr[lc(x)].sz >= k)
      x = lc(x);
    else if (tr[lc(x)].sz + tr[x].cnt >= k)
      break;
    else
      k -= tr[lc(x)].sz + tr[x].cnt, x = rc(x);
  }
  splay(x, 0);
  return tr[x].val;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  insert(-1e9), insert(1e9);
  int n, op, x;
  cin >> n;
  while (n--) {
    cin >> op >> x;
    if (op == 1)
      insert(x);
    else if (op == 2)
      del(x);
    else if (op == 3)
      cout << get_rk(x) << endl;
    else if (op == 4)
      cout << get_val(x + 1) << endl;
    else if (op == 5)
      cout << tr[get_pre(x)].val << endl;
    else
      cout << tr[get_suc(x)].val << endl;
  }
  return 0;
}